package com.teamtgv.shareshark;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.teamtgv.shareshark.task.ServerReachableTask;

public class InternetChecker {

	private Context context;
	
	public InternetChecker(Context context) {
		this.context = context;
	}
	
	public boolean isOnline() {
	    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo netInfo = cm.getActiveNetworkInfo();
	    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
	        return true;
	    }
	    return false;
	}
	
	public boolean serverReachable() {
		ServerReachableTask serverReachableTask = new ServerReachableTask();
		serverReachableTask.execute();
		try {
			return serverReachableTask.get();
		} catch (Exception e) {
			Log.v("InternetChecker", "Error during executing task to check server reachable: " + e.toString() + ": " + e.getMessage());
			return false;
		}
	}
}
